﻿using System;
using System.Collections.Generic;
using System.Text;
using Mighty.Common;

namespace Mighty.Brain
{
    public class TMBrain : BasicBrain
    {
        IsMnJ mnj = new IsMnJ();

        private class IsMnJ
        {
            public bool isJoker;
            public bool isMightySpade;
            public bool isMightyDia;
            public Shape mightyShape;
            public bool isMighty;

            public IsMnJ()
            {
                mightyShape = Shape.Spade;
            }
        }

        public override Betting BettingTicEvent()
        {
            

            Betting betting = new Betting();

            betting.isPass = true;

            int[] countCards = new int[4];

            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == Shape.Clover) countCards[(int)Shape.Clover] += CountBetting(card);
                else if (card.shape == Shape.Heart) countCards[(int)Shape.Heart] += CountBetting(card);
                else if (card.shape == Shape.Spade) countCards[(int)Shape.Spade] += CountBetting(card);
                else if (card.shape == Shape.Diamond) countCards[(int)Shape.Diamond] += CountBetting(card);
            }

            int girudaShape = -1;
            int count = 0;

            for(int i = 0; i<4; i++)
            {
                if (countCards[i] > count)
                {
                    count = countCards[i];
                    girudaShape = i;
                }
            }

            if (girudaShape == (int)Shape.Spade)
                mnj.mightyShape = Shape.Diamond;

            if (mnj.mightyShape == Shape.Diamond)
            {
                if (mnj.isJoker && mnj.isMightyDia)
                    return betting;
            }
            else
            {
                if (mnj.isJoker && mnj.isMightySpade)
                    return betting;
            }

            if (count >= 50)
            {
                betting.isPass = false;
                betting.giruda = (Giruda)girudaShape;
                betting.limit = count / 10 + 8;
            }

            if (gameInfo.betting.limit >= betting.limit)
                betting.isPass = true;

            return betting;
        }

        private int CountBetting(HandedCard card)
        {
            if (card.shape == Shape.Joker)
            {
                mnj.isJoker = true;
                return 20;
            }
            if (card.shape == Shape.Spade)
            {
                if (card.number == 14)
                {
                    mnj.isMightySpade = true;
                    return 20;
                }
            }
            if (card.shape == Shape.Diamond)
            {
                if (card.number == 14)
                {
                    mnj.isMightyDia = true;
                    return 20;
                }
            }
            
            if (card.number == 14)
                return 20;
            if (card.number == 13)
                return 15;
            if (card.number == 12)
                return 15;
            if (card.number == 11)
                return 13;
            if (card.number == 10)
                return 12; 
            return 10;
        }

        public override FriendPlayer ChoiceFriendEvent()
        {
            Card friendCard = new Card(); ;

            if (mnj.mightyShape == Shape.Spade)
                if (mnj.isMightySpade)
                    mnj.isMighty = true;
            if (mnj.mightyShape == Shape.Diamond)
                if (mnj.isMightyDia)
                    mnj.isMighty = true;
            if (!mnj.isMighty)
            {
                friendCard.number = 14;
                friendCard.shape = mnj.mightyShape;
                gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                gameInfo.friend.cardCondition = friendCard;
            }
            else if (!mnj.isJoker)
            {
                friendCard.shape = Shape.Joker;
                gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                gameInfo.friend.cardCondition = friendCard;
            }
            else
            {
                gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.firstwin;
            }
            return gameInfo.friend;
        }

        public override HandedCard RoundTicEvent()
        {
            Dictionary<int, int> winrate = new Dictionary<int, int>();

            int j = 0;
            foreach (HandedCard card in gameInfo.myCards)
            {
                winrate.Add(j, WinRateCalc(card));
                j++;

            }

            int throwCard = 0;
            int maxRate = -1;

            for (int i = 0; i < 10; i++)
            {
                if (winrate[i] > maxRate)
                {
                    throwCard = i;
                    maxRate = winrate[i];
                }
            }

            return gameInfo.myCards[throwCard];
        }

        /*private int WinRateCalc(HandedCard card)
        {

            if (card.isPossible == false)
                return -1;

            int winrate = 0;
            if (gameInfo.attackPlayer.position == gameInfo.myInfo.position)
            {
                //gameInfo.betting.giruda;
                if ((int)card.shape == (int)gameInfo.betting.giruda)
                    winrate += 10;
            }
            else
            {
                if(gameInfo.roundIdx > 0)
                    if ((int)card.shape == (int)gameInfo.betting.giruda)
                        winrate += 10;
            }
            if (card.number == 14)
                winrate += 9;
            if (card.number == 13)
                winrate += 7;
            if (card.number == 12)
                winrate += 4;
            if (card.number == 11)
                winrate += 2;
            if (card.number == 10)
                winrate += 1;

            return winrate;
        }*/

        private int WinRateCalc(HandedCard card)
        {
            if (card.isPossible == false)
                return -1;

            int winrate = 0;
            //gameInfo.betting.giruda;
            if ((int)card.shape == (int)gameInfo.betting.giruda)
                winrate += 10;
            if (card.number == 14)
                winrate += 9;
            if (card.number == 13)
                winrate += 7;
            if (card.number == 12)
                winrate += 4;
            if (card.number == 11)
                winrate += 2;
            if (card.number == 10)
                winrate += 1;

            return winrate;
        }
    }
}
